package com.windmill.core;

import iqq.im.QQActionListener;
import iqq.im.QQException;
import iqq.im.QQNotifyListener;
import iqq.im.WebQQClient;
import iqq.im.actor.QQActorDispatcher;
import iqq.im.bean.QQBuddy;
import iqq.im.bean.QQGroupMember;
import iqq.im.bean.QQMsg;
import iqq.im.bean.QQUser;
import iqq.im.core.QQModule;
import iqq.im.event.QQActionEvent;
import iqq.im.event.QQActionEvent.Type;
import iqq.im.event.QQActionFuture;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.windmill.module.CardModule;

public class MargicCardClient extends WebQQClient implements CardClient{

	private static final Logger LOG = LoggerFactory.getLogger(MargicCardClient.class);
	public CardStore getCardStore() {
		return cardStore;
	}
	public void setCardStore(CardStore cardStore) {
		this.cardStore = cardStore;
	}
	public DataStore getDataStore() {
		return dataStore;
	}
	public void setDataStore(DataStore dataStore) {
		this.dataStore = dataStore;
	}
	private CardStore cardStore;
	private DataStore dataStore;
	public MargicCardClient(String username, String password,
			QQNotifyListener notifyListener, QQActorDispatcher actorDispatcher) {
		super(username, password, notifyListener, actorDispatcher);
		initStore();
	}

	private void initStore() {
		this.cardStore = new CardStore();
		CardModule mod = this.getModule(QQModule.Type.CARD);
		mod.getCardInfoV3(cardStore,new QQActionListener() {			
			public void onActionEvent(QQActionEvent event) {
				if(event.getType()==Type.EVT_OK){					
					dataStore = new DataStore(cardStore);
					dataStore.init("GroupUserData.fc","ChangeCardData.txt");
					LOG.debug("初始化 CardStore and DateStore 完成。");
				}
			}
		});
	}
	/**
	 * 包装消息，找群成员QQ号及CardName
	 * @throws QQException 
	 */
	public QQGroupMember getGroupMember(QQMsg msg) throws QQException {
		final QQGroupMember member = msg.getGroup().getMemberByUin(msg.getFrom().getUin());
		int memberCount = msg.getGroup().getMembers().size();
		if(member.getCard()==null||"".equals(member.getCard())){
			QQActionEvent event = getGroupInfo(msg.getGroup(), null).waitFinalEvent();			
			if(event.getType()==Type.EVT_OK){
				member.setNickname(member.getCard());
				if(memberCount>10)member.setNewMember(true);
				LOG.debug(msg.getGroup().getName()+"：群名片加载成功。");
			}	
		}
		if(member.getQQ()==0){			
			QQActionEvent event = getUserQQ(member,null).waitFinalEvent();
			if(event.getType()==Type.EVT_OK){						
				member.setQQ(member.getQQ());
			}						
		}
		member.setNickname(member.getCard());
		msg.setFrom(member);
		return member;
	}
	/**
	 * 通过uin找QQ
	 * @throws QQException 
	 */
	public Long getQQ(QQMsg msg) throws QQException{
		QQBuddy buddy = getStore().getBuddyByUin(msg.getFrom().getUin());
		if(buddy==null){
			QQActionEvent event = getUserQQ(msg.getFrom(),null).waitFinalEvent();
			if(event.getType()==Type.EVT_OK){
				QQUser user = (QQUser) event.getTarget();	
				buddy = new QQBuddy();
				buddy.setUin(user.getUin());
				buddy.setQQ( user.getQQ());
				buddy.setNickname("");
				getStore().addBuddy(buddy);
				msg.setFrom(buddy);
			}			
		}
		return buddy.getQQ();	
	}
	@Override
	public QQActionFuture getCardInfoV3(QQActionListener qqActionListener) {
		CardModule mod = getModule(QQModule.Type.CARD);
		return mod.getCardInfoV3(this.cardStore,qqActionListener);
	}
	@Override
	public QQActionFuture getUserMainCardInfo(String descQQ,QQActionListener qqActionListener) {
		CardModule mod = getModule(QQModule.Type.CARD);
		return mod.getUserMainCardInfo(descQQ,this,qqActionListener);
	}
	@Override
	public QQActionFuture getCardCollection(String descQQ, QQActionListener qqActionListener) {
		CardModule mod = getModule(QQModule.Type.CARD);
		return mod.getCardCollection(descQQ,this,qqActionListener);
	}
	@Override
	public QQActionFuture getCardHonor(String descQQ,QQActionListener qqActionListener) {
		CardModule mod = getModule(QQModule.Type.CARD);
		return mod.getCardHonor(descQQ,this,qqActionListener);
	}
	

	

}
